﻿using System.Collections.ObjectModel;
using K9Tools.Abstractions.Navigation;

namespace K9Tools.ViewModels;

public class MainWindowViewModel : BindableBase
{
    private readonly IRegionManager _regionManager;

    public MainWindowViewModel(IRegionManager regionManager)
    {
        MenuChangeCommand = new DelegateCommand<NavigationViewItem>(HandleMenuChanged);
        _regionManager = regionManager;
    }

    public ObservableCollection<NavigationViewItem> Menus { get; } = new();

    public DelegateCommand<NavigationViewItem> MenuChangeCommand { get; }

    private NavigationViewItem? _selectedMenu;
    public NavigationViewItem? SelectedMenu
    {
        get => _selectedMenu;
        set => SetProperty(ref _selectedMenu, value, () => HandleMenuChanged(value));
    }

    internal void Load()
    {
        var menus = ContainerLocator.Container.Resolve<INavigationManager>().Menus;
        this.Menus.AddRange(menus);
    }

    private void HandleMenuChanged(NavigationViewItem? item)
    {
        var view = item?.View;
        if(string.IsNullOrWhiteSpace(view))
        {
            return;
        }
        _regionManager.RequestNavigate("ContentRegion", view);
    }
}
